package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Cargo;
import pojo.Warehouse;
import service.CargoManager;
import service.WarehouseManager;
import service.factory.ManagerFactory;


@WebServlet("/cargo/cargo")
public class CargoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CargoManager cargoManager;
	private WarehouseManager warehouseManager;
       
    public CargoServlet() {
        super();
        cargoManager = ManagerFactory.getCargoManagerInstance();
        warehouseManager = ManagerFactory.getWarehouseManagerInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String w_id = request.getParameter("w_id");
		if (w_id==null) {
			request.getRequestDispatcher("../error?info=warehouse not exist").forward(request, response);
			return;
		}
//		List<Warehouse> warehouses = warehouseManager.getAllWarehouses();
//		request.setAttribute("warehouse", warehouses);
		boolean isHK = false;
		boolean isTJ = false;
		boolean isSH = false;
		Warehouse warehouse = warehouseManager.getWarehouse(w_id);
		if (warehouse==null) {
			request.getRequestDispatcher("../error?info=warehouse not exist").forward(request, response);
			return;
		}
		if (warehouse.getAddr().equals("天津")) {
			isTJ=true;
		}
		if (warehouse.getAddr().equals("香港")) {
			isHK=true;
		}
		if (warehouse.getAddr().equals("上海")) {
			isSH=true;
		}
		request.setAttribute("isTJ", isTJ);
		request.setAttribute("isHK", isHK);
		request.setAttribute("isSH", isSH);
		List<Cargo> cargos = cargoManager.getByWarehouse(w_id);
		request.setAttribute("cargos", cargos);
		request.getRequestDispatcher("cargo.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
